//
//  SRLoginViewController.m
//  SportRing
//
//  Created by Apple on 14/10/23.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRLoginViewController.h"
#import "SRLoginView.h"
#import "SRAccountNetService.h"
#import "SRDB.h"
#import "SRManager.h"
#import "Utils.h"
#import "SRMainViewController.h"
#import "Utils.h"

@interface SRLoginViewController () <SRLoginViewDelegate>

@property (nonatomic, weak) IBOutlet SRLoginView * loginView;

@end

@implementation SRLoginViewController

-  (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.edgesForExtendedLayout=UIRectEdgeNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    [self.view endEditing:YES];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    if (_loginView.center.y < self.view.center.y) {
        return;
    }
    //得到键盘高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    
    [UIView animateWithDuration:0.2 animations:^{
        _loginView.center = CGPointMake(_loginView.center.x, self.view.center.y - keyboardRect.size.height);
    }];
    
   
}

- (void)keyboardwillChange:(NSNotification *)notification
{
    //得到键盘高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    [UIView animateWithDuration:0.3 animations:^{
        _loginView.center = CGPointMake(_loginView.center.x, self.view.center.y - keyboardRect.size.height);
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    
    _loginView.center = self.view.center;
}

#pragma mark - SRLoginViewDelegate

- (void)doLoginAction:(NSString *)mobile password:(NSString *)password
{
    [self.view endEditing:YES];
    [SVProgressHUD show];
    
//    @weakify(self);
    
    [[SRAccountNetService loginWithUserName:mobile
                                   userPwd:password]
     subscribeNext:^(id x) {
         
         //登陆成功保存userToken
         SRMe *me = x;
         [Utils saveUserLoginToken:me];
         
         [SVProgressHUD showSuccessWithStatus:@"登录成功"];
         SRDevice * device = [SRDevice findDeviceForMe:[SRMe currentUser]];
         [[SRManager sharedInstance] setCurrentPeripheralName:device.name];
         [self dismissViewControllerAnimated:NO completion:nil];
         
         [[SRManager sharedInstance] readyToStartUp];
     } error:^(NSError *error) {
         if (error.code == 2003) {
             [SVProgressHUD showErrorWithStatus:@"密码错误"];
         }else
         {
             [SVProgressHUD showErrorWithStatus:@"登录失败"];
         }
         
     }];
}

@end
